package com.ap.core.business.message.domain.translators.impl;

import org.joda.time.LocalDateTime;

import com.ap.core.business.message.domain.translators.BindingListener;
import com.ap.core.business.message.domain.translators.BindingMetaData;
import com.ap.core.business.message.domain.translators.Translator;
import com.ap.core.common.types.InputMessageType;
import com.ap.core.common.types.MessageStatus;
import com.ap.core.cxf.jaxb.managepayment.RetrieveRTPMessageType;
import com.ap.core.dao.BasketReferenceDao;
import com.ap.core.dao.FinancialInstitutionDao;
import com.ap.core.dao.MessageTimeOutDao;
import com.ap.core.dao.OrderPaymentDao;
import com.ap.core.dao.ZappConsumerDao;
import com.ap.core.domain.FinancialInstitution;
import com.ap.core.domain.MessageTimeOut;
import com.ap.core.domain.ZappConsumer;
import com.ap.core.message.managepayment.info.RetrieveRTPMessageInfo;

/**
 * User: Ranjit.Singh
 * Implementation class for {@link com.ap.core.business.message.domain.translators.Translator} to translate RetrieveRTP1Request bound object to OrderPayment domain entity.
 */
public class RetrieveRTPTranslator implements Translator<RetrieveRTPMessageInfo> {

    private ZappConsumerDao zappConsumerDao;
    private OrderPaymentDao orderPaymentDao;
    private FinancialInstitutionDao financialInstitutionDao;
    private BasketReferenceDao basketReferenceDao;
    private MessageTimeOutDao messageTimeOutMasterDao;

    @Override
    public void translate(RetrieveRTPMessageInfo boundObject, BindingMetaData metaData, BindingListener bindingListener) {
        final String fiConsumerId = boundObject.getRequestInfo().getFIConsumerId();
        final ZappConsumer zappConsumer = zappConsumerDao.getZappConsumerByFICustomerId(fiConsumerId);
        final FinancialInstitution fi = financialInstitutionDao.getFIByIdentification(boundObject.getRequestInfo().getFIId());
        MessageTimeOut messageTimeOut = messageTimeOutMasterDao.findById(RetrieveRTPMessageType.RETRIEVERTP.name());
        LocalDateTime confirmationExpiryTime = new LocalDateTime().plusSeconds(messageTimeOut.getTimeOutSeconds());
        orderPaymentDao.updateOrderPayment(metaData.getOriginalOrderPaymentId(), MessageStatus.AWAITING_PAYMENT_CONFIRMATION, zappConsumer.getId(), fi.getId(), metaData.getApMessageId(), fiConsumerId, confirmationExpiryTime);
        
        if(InputMessageType.SUBMITRTPFORBRN.equals(metaData.getOriginalReadOnlyOrderPayment().getInputMessageType())){
           basketReferenceDao.updateBRNRetrieved(metaData.getOriginalOrderPaymentId());
        }
    }

    public void setZappConsumerDao(ZappConsumerDao zappConsumerDao) {
        this.zappConsumerDao = zappConsumerDao;
    }

    public void setFinancialInstitutionDao(FinancialInstitutionDao financialInstitutionDao) {
        this.financialInstitutionDao = financialInstitutionDao;
    }

    public void setOrderPaymentDao(OrderPaymentDao orderPaymentDao) {
        this.orderPaymentDao = orderPaymentDao;
    }

    public void setBasketReferenceDao(BasketReferenceDao basketReferenceDao) {
        this.basketReferenceDao = basketReferenceDao;
    }

	public void setMessageTimeOutMasterDao(MessageTimeOutDao messageTimeOutMasterDao) {
		this.messageTimeOutMasterDao = messageTimeOutMasterDao;
	}

}
